/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var minDiffInBST = function(root) {
  let nodeArr =[];
  function preView(node) {
    if(!node) return;
    nodeArr.push(node.val);
    preView(node.left);
    preView(node.right);
  }
  preView(root);
  nodeArr.sort((a, b) => a - b);
  let min = Math.abs(nodeArr[0] - nodeArr[1]);
  if(nodeArr.length == 2) return min;
  for(let i = 1; i < nodeArr.length; i++) {
    let value = Math.abs(nodeArr[i - 1] - nodeArr[i]);
    if(value < min) min = value;
  }
  return min;
};
